package com.project.service.impl;

import com.project.bean.CutProductBean;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

/**
 * 商品业务层实现类
 */
public class ProductServiceImpl extends BaseService implements IProductService {

    @Override
    public void add(ProductBean productBean) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.add(productBean);
        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id,int price) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.update(id,price);
        session.commit();
        session.close();
    }

    @Override
    public ProductBean findById(int id) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        ProductBean productBean =  mapper.findById(id);
        session.close();
        return productBean;
    }

    @Override
    public int countProduct() {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        int count = mapper.countProduct();
        session.close();
        return count;

    }

    @Override
    public CutProductBean<ProductBean> cutAll(int pageNO) {
        CutProductBean<ProductBean> cutBean = new CutProductBean<ProductBean>();
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        //得到当前页的数据
        List<ProductBean> list = mapper.cutAll((pageNO - 1) * CutProductBean.PAGESIZE ,
                CutProductBean.PAGESIZE);
        cutBean.setList(list);

        //得到总记录数
        cutBean.setCount(mapper.countProduct());

        session.close();
        return cutBean;

    }

    @Override
    public CutProductBean<ProductBean> cutByAddress(int pageNO, String address, LocalDate startDate, LocalDate endDate) {
        CutProductBean<ProductBean> cutBean = new CutProductBean<>();
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        List<ProductBean> list = mapper.cutByAddress(address,startDate,endDate,
                (pageNO - 1)*CutProductBean.PAGESIZE,CutProductBean.PAGESIZE);
        cutBean.setList(list);

        cutBean.setCount(mapper.countByAddress(address,startDate,endDate));

        session.close();
        return cutBean;
    }


    @Override
    public CutProductBean<ProductBean> cutByName(int pageNO, String name, int startPrice, int endPrice) {
        CutProductBean<ProductBean> cutBean = new CutProductBean<>();
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        List<ProductBean> list = mapper.cutByName(name,startPrice,endPrice,
                (pageNO - 1)*CutProductBean.PAGESIZE,CutProductBean.PAGESIZE);
        cutBean.setList(list);

        cutBean.setCount(mapper.countByName(name,startPrice,endPrice));

        session.close();
        return cutBean;
    }
}
